package com.example.neusoftfood_828lhl.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.neusoftfood_828lhl.R;

public class BaseFragment extends Fragment {
    private SharedPreferences sp;
    protected FragmentManager childManager;
    private FragmentTransaction transaction;
    protected Context context;
    public BaseFragment() {
        context = getActivity();
    }
    public String getUser_id() {
        sp = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getString("user_id", "");
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        childManager = getChildFragmentManager();
    }
    protected void addFragment(Fragment fragment) {
        transaction = childManager.beginTransaction();
        transaction.add(R.id.fragment_container, fragment);
        transaction.commit();
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }
    protected void replaceFragment(Fragment fragment) {
        transaction = childManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
    }
    protected void removeFragment(Fragment fragment) {
        transaction = childManager.beginTransaction();
        transaction.remove(fragment);
        transaction.commit();
    }
}
